<?php
namespace Talk\Controller;
//加载GatewayClient。关于GatewayClient参见本页面底部介绍
require_once './ThinkPHP/Library/Vendor/GatewayClient/Gateway.php';
// GatewayClient 3.0.0版本开始要使用命名空间
use GatewayClient\Gateway;
use Wx\Controller\CommonController;

class TalkMsgController extends CommonController {

    // 聊天主界面
    public function index(){
        $uid = session("u_id");

        $to_uid = I('to_uid');
        $to_group = M("member")->where("id = {$uid}")->getField('c_id');//小区
        if($to_uid){
            $conf = M('conf')->where('id=4')->find();   //查询聊天数据库信息
            $preson = M('person','talk_',$conf['value']);     //实例化聊天数据库中的个人聊天表
            $all_preson = $preson->where("from_uid = {$uid} OR to_uid = {$uid}")->order('addtime DESC')->select();//所有聊天记录
            $all_uid = array();
            foreach($all_preson as $ak => $av){
                if($av['from_uid'] == $uid){
                    $all_uid[] = $av['to_uid'];
                }else{
                    $all_uid[] = $av['from_uid'];
                }//end if
            }
//            $all_uid = array_unique($all_uid);//去重
            foreach(array_unique($all_uid) as $k => $v){
                $member[$v] = M("member")->where('id = '.$v)->find();//用户信息
            }
            $this->assign('to_uid',$to_uid);
        }else{
            $mem_id = M("member")->where('c_id = '.$to_group)->field('id')->select();//该小区用户
            $member = array();
            foreach($mem_id as $k => $v){
                if($v['id'] != $uid){
                    $member[$v['id']] = M("member")->where('id = '.$v['id'])->find();//用户信息
                }
            }
            $this->assign('to_group',$to_group);
        }//end if

        $type = $to_uid ? 'toUser':'toGroup';//区分个人聊天，群聊
//        $member = M("member")->where('id = ')
        // 设置GatewayWorker服务的Register服务ip和端口
        Gateway::$registerAddress = '127.0.0.1:1303';
        // 查询用户是否已经绑定在线了
        $isOnline = Gateway::getClientIdByUid($uid);
//        dump($isOnline);
        $this->assign('uid',$uid);
        $this->assign('member',$member);
        $this->assign('type',$type);
        $this->assign('isOnline',$isOnline);
        $this->display("TalkMsg");
    }

    //发送给单人
    public function toUser(){
        Gateway::$registerAddress = '127.0.0.1:1303';
//        $to_uid = 1391;
        $to_uid =  $_POST['to_uid'];
        $message = $_POST['content'];
        $data['msg'] = $message;
        $data['from_uid'] = session('u_id');
        $data['to_uid'] = $to_uid;
        Gateway::sendToUid($to_uid, json_encode($data)); //发给对方
        $conf = M('conf')->where('id=4')->find();   //查询聊天数据库信息
        $preson = M('person','talk_',$conf['value']);     //实例化聊天数据库中的个人聊天表
        $data['addtime'] = time();
        $preson->add($data);//添加聊天记录
//        Gateway::sendToUid($data['from_uid'], json_encode($data)); //发给自己

    }
    //发送给单人
    public function toGroup(){
        Gateway::$registerAddress = '127.0.0.1:1303';
//        $to_uid = 1391;
        $to_group =  $_POST['to_group'];
        $message = $_POST['content'];
        $data['msg'] = $message;
        $data['from_uid'] = session('u_id');
        $data['group_id'] = $to_group;
        Gateway::sendToGroup($to_group, json_encode($data)); //发给对方
        $conf = M('conf')->where('id=4')->find();   //查询聊天数据库信息
        $preson = M('group','talk_',$conf['value']);     //实例化聊天数据库中的个人聊天表
        $data['addtime'] = time();
        $preson->add($data);//添加聊天记录

    }


}